package dao.daoimpl;

import util.DataBase;
import dao.LabelDao;
import bean.Label;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class LabelDaoImpl implements LabelDao {
    @Override
    public boolean addLabel(ArrayList<Label> labels,int articleId) {
        DataBase.init();
        for(Label label:labels){
            String sql="insert into article_label (label_id,article_id) values ('"+label.getId()+"','"+articleId+"')";
            int flag= DataBase.operate(sql);
            if(flag<0){
                DataBase.release();
                return false;
            }
        }
        DataBase.release();
        return true;
    }

    @Override
    public boolean updateLabel(Label label) {
        String sql="update label_table set label_content='"+label.getContent()+"',label_time='"+label.getTime()+"'where label_id='"+label.getId()+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.init();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteLabel(int labelId) {
        DataBase.init();
        String sql="delete from label_table where label_id='"+labelId+"'";
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteArticleLabel(int articleId) {
        DataBase.init();
        String sql="delete from article_label where article_id='"+articleId+"'";
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

//    @Override
//    public boolean deleteLabel(int articleId) {
//        String sql="delete from label_table where label_articleId= '"+articleId+"'";
//        DataBase.init();
//        int flag=DataBase.operate(sql);
//        DataBase.release();
//        if(flag>0){
//            return true;
//        }
//        return false;
//    }

    @Override
    public ArrayList<Label> getLabels(int articleId) throws SQLException {
        ArrayList<Label> labels=new ArrayList<>();
        String sql="select label_table.label_content,label_table.label_time,label_table.label_id,label_table.label_user from label_table,article_label where label_table.label_id=article_label.label_id and article_label.article_id='"+articleId+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            String content=resultSet.getString("label_content");
            String time=resultSet.getString("label_time");
            int id=resultSet.getInt("label_id");
            String user=resultSet.getString("label_user");
            labels.add(new Label(id,content,time,user));
        }
        DataBase.release();
        return labels;
    }

    @Override
    public ArrayList<Label> getAllLabels() throws SQLException {
        ArrayList<Label> labels=new ArrayList<>();
        String sql="select * from label_table";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while(resultSet.next()){
            int id=resultSet.getInt("label_id");
            String content=resultSet.getString("label_content");
            String time=resultSet.getString("label_time");
            String user=resultSet.getString("label_user");
            labels.add(new Label(id,content,time,user));
        }
        DataBase.release();
        return labels;
    }

    @Override
    public ArrayList<Label> getUserLabels(User user) throws SQLException {
        ArrayList<Label> labels=new ArrayList<>();
        String sql="select * from label_table where label_user='"+user.getAccount()+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while(resultSet.next()){
            int id=resultSet.getInt("label_id");
            String content=resultSet.getString("label_content");
            String time=resultSet.getString("label_time");
            String userAccount=resultSet.getString("label_user");
            labels.add(new Label(id,content,time,userAccount));
        }
        DataBase.release();
        return labels;
    }

    @Override
    public boolean addLabel(Label label) {
        String sql="insert into label_table (label_content,label_time,label_user) values ('"+label.getContent()+"','"+label.getTime()+"','"+label.getUser()+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }
}
